# -*-coding:utf-8 -*-
'''
Created on 2011-10-26

@author: tony.li.fly@gmail.com
'''
from xyh.oa.models import Contact
from django.http import HttpResponse
from django.utils import simplejson as json

oftype = [{'code':'zj','name':'直接公司'},{'code':'th','name':'同行公司'},{'code':'fh','name':'发货公司'},
          {'code':'sh','name':'收货公司'},{'code':'tz','name':'通知公司'},{'code':'cgs','name':'船公司'},
          {'code':'hk','name':'航空公司'},{'code':'hwdl','name':'海外代理'},{'code':'jndl','name':'境内代理'},
          {'code':'dcdl','name':'订舱代理'},{'code':'tch','name':'拖车行'},{'code':'ckcz','name':'仓库/场站'},
          {'code':'bgh','name':'报关行'},{'code':'bjh','name':'报检行'},{'code':'jydw','name':'经营单位'},
          {'code':'bc','name':'驳船公司'},{'code':'bx','name':'保险公司'},{'code':'kd','name':'快递公司'},{'code':'qt','name':'其它'}]
          

def types(request):
    return HttpResponse(json.dumps(oftype)) 

def listContacts(request):
    if request.GET.get('oftype',False) :
        contacts = Contact.objects.filter(oftype=request.GET['oftype']);
    else :
        contacts = Contact.objects.all();
    contactsJson = {'contacts':[]};
    for contact in contacts:
        contactsJson['contacts'].append({'id':contact.id,'name':contact.name,'code':contact.code,'person':contact.person,'phone':contact.person,'phone':contact.phone,'fax':contact.fax,'address':contact.address,'oftype':contact.oftype})
    return HttpResponse(json.dumps(contactsJson))

def delContact(request,contact_id):
    contact = Contact.objects.get(pk=contact_id)
    contact.delete()
    return HttpResponse()
    
def addContact(request):
    contact = Contact(name=request.POST['name'],oftype=request.POST['oftype'],code=request.POST['code'],person=request.POST['person'],phone=request.POST['person'],fax=request.POST['fax'],address=request.POST['address'])
    contact.save()
    return HttpResponse()

def updateContact(request):
    contact = Contact.objects.get(pk = request.POST['id'])
    contact.name = request.POST['name']
    contact.code = request.POST['code']
    contact.oftype = request.POST['oftype']
    contact.person = request.POST['person']
    contact.phone = request.POST['phone']
    contact.fax = request.POST['fax']
    contact.address = request.POST['address']
    contact.save()
    return HttpResponse()